<?php

namespace App\Services\Commission;

use App\Constant\CommissionEnum;
use App\Constant\Enum;
use App\Models\Commission;
use App\Models\GroupMember;
use App\Models\Setting;
use App\Models\User;
use App\Models\UserWithdraw;
use App\Services\Wallet\UserWalletService;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class CommissionService
{

    public static function distribute(UserWithdraw $userWithdraw)
    {
        $group = Setting::get('group');


        //先判断用户是否已经发放过佣金
        $Commission = Commission::query()
            ->where([
                'child_user_withdraw_id' => $userWithdraw->id,
            ])
            ->first();

        if (!empty($Commission)) {
            Log::error('佣金已经发放过' . $Commission->id);
            return;
        }
//提现用户
        /** @var User $childUser */
        $childUser = $userWithdraw->user;
//提现用户的上1级用户
        /** @var User $parentUser */
        $parentUser = $childUser->parent;
        // 无上1级
        if (empty($parentUser)) {
            return;
        }
        //当前提现金币数
        DB::beginTransaction();
        //提现用户的上级用户的上级用户  等于  提现用户
        if ($parentUser->parent_id == $childUser->id) {
            Log::error(sprintf('无效用户对应关系Users:(%s, %s)', $childUser->id, $parentUser->parent_id));
            return;
        }
        $exchangeRate = Setting::get('exchange_rate');
        //  DB::beginTransaction();
        try {

            //上级是高级团长
            if ($parentUser->is_advanced_group == 1) {
                //当前提现用户上级是高级团长，
                $group_cash = $group['commissions3_1'];//
                $amount = $userWithdraw->amount;//提现金币
                $commissionAttributes = [
                    'user_id' => $parentUser->getAttribute('id'),//上级用户id
                    'child_user_id' => $childUser->getAttribute('id'),//提现用户
                    'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                    'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                    'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                    'exchange_rate' => $exchangeRate,//兑换比例
                    'amount' => $amount * $group_cash,//奖励金币数
                    'prize' => $group_cash,//佣金比例
                    'child_user_withdraw_at' => Carbon::now(),//时间
                    'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                    'remark' => '一级高级团员提现高级团长抽佣比例' . $group_cash,
                ];
                Commission::query()->create($commissionAttributes);
                UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);
            } else if ($parentUser->is_group_leader == 1) {
                //当前提现用户上级是普通团长，
                $group_cash = $group['commissions2_1'];//
                $amount = $userWithdraw->amount;//提现金币
                $commissionAttributes = [
                    'user_id' => $parentUser->getAttribute('id'),//上级用户id
                    'child_user_id' => $childUser->getAttribute('id'),//提现用户
                    'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                    'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                    'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                    'exchange_rate' => $exchangeRate,//兑换比例
                    'amount' => $amount * $group_cash,//奖励金币数
                    'prize' => $group_cash,//佣金比例
                    'child_user_withdraw_at' => Carbon::now(),//时间
                    'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                    'remark' => '一级普通团员提现普通团长抽佣比例' . $group_cash,
                ];
                Commission::query()->create($commissionAttributes);
                UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);
            } else {
                //判断上2级
                $parentUsertwo = $parentUser->parent;
                if (!empty($parentUsertwo)) {

                    //当前提现用户上级不是团长，上级是高级团长
                    if ($parentUsertwo->is_advanced_group == 1) {


                        $group_cash = $group['commissions3_1'];//
                        $amount = $userWithdraw->amount;//提现金币
                        $commissionAttributes = [
                            'user_id' => $parentUser->getAttribute('id'),//上级用户id
                            'child_user_id' => $childUser->getAttribute('id'),//提现用户
                            'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                            'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                            'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                            'exchange_rate' => $exchangeRate,//兑换比例
                            'amount' => $amount * $group_cash,//奖励金币数
                            'prize' => $group_cash,//佣金比例
                            'child_user_withdraw_at' => Carbon::now(),//时间
                            'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                            'remark' => '二级高级团员提现一级团员抽佣比例' . $group_cash,
                        ];
                        Commission::query()->create($commissionAttributes);
                        UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                        $group_cash = $group['commissions3_2'];//
                        $amount = $userWithdraw->amount;//提现金币
                        $commissionAttributes = [
                            'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                            'child_user_id' => $childUser->getAttribute('id'),//提现用户
                            'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                            'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                            'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                            'exchange_rate' => $exchangeRate,//兑换比例
                            'amount' => $amount * $group_cash,//奖励金币数
                            'prize' => $group_cash,//佣金比例
                            'child_user_withdraw_at' => Carbon::now(),//时间
                            'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                            'remark' => '二级高级团员提现高级团长抽佣比例' . $group_cash,
                        ];
                        Commission::query()->create($commissionAttributes);
                        UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);

                    } else if ($parentUsertwo->is_group_leader == 1) {

                        //当前提现上级普通1级团员拿15，上上级 ，普通团长拿12，

                        $group_cash = $group['commissions2_1'];//
                        $amount = $userWithdraw->amount;//提现金币
                        $commissionAttributes = [
                            'user_id' => $parentUser->getAttribute('id'),//上级用户id
                            'child_user_id' => $childUser->getAttribute('id'),//提现用户
                            'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                            'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                            'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                            'exchange_rate' => $exchangeRate,//兑换比例
                            'amount' => $amount * $group_cash,//奖励金币数
                            'prize' => $group_cash,//佣金比例
                            'child_user_withdraw_at' => Carbon::now(),//时间
                            'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                            'remark' => '二级普通团员提现一级普通团员抽佣比例' . $group_cash,
                        ];
                        Commission::query()->create($commissionAttributes);
                        UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                        $group_cash = $group['commissions2_2'];//
                        $amount = $userWithdraw->amount;//提现金币
                        $commissionAttributes = [
                            'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                            'child_user_id' => $childUser->getAttribute('id'),//提现用户
                            'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                            'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                            'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                            'exchange_rate' => $exchangeRate,//兑换比例
                            'amount' => $amount * $group_cash,//奖励金币数
                            'prize' => $group_cash,//佣金比例
                            'child_user_withdraw_at' => Carbon::now(),//时间
                            'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                            'remark' => '二级普通团员提现普通团长抽佣比例' . $group_cash,
                        ];
                        Commission::query()->create($commissionAttributes);
                        UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);
                    } else {
                        //判断上3级
                        $parentUserthree = $parentUsertwo->parent;
                        if (!empty($parentUserthree)) {
                            if ($parentUserthree->is_advanced_group == 1) {

                                //当前提现用户上级高级2级团员，上上级高级1级团员，上上级高级团长

                                $group_cash = $group['commissions3_2'];//
                                $amount = $userWithdraw->amount;//提现金币
                                $commissionAttributes = [
                                    'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                    'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                    'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                    'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                    'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                    'exchange_rate' => $exchangeRate,//兑换比例
                                    'amount' => $amount * $group_cash,//奖励金币数
                                    'prize' => $group_cash,//佣金比例
                                    'child_user_withdraw_at' => Carbon::now(),//时间
                                    'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                    'remark' => '三级高级团员提现二级团员抽佣比例' . $group_cash,
                                ];
                                Commission::query()->create($commissionAttributes);
                                UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                $group_cash = $group['commissions3_1'];//
                                $amount = $userWithdraw->amount;//提现金币
                                $commissionAttributes = [
                                    'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                    'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                    'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                    'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                    'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                    'exchange_rate' => $exchangeRate,//兑换比例
                                    'amount' => $amount * $group_cash,//奖励金币数
                                    'prize' => $group_cash,//佣金比例
                                    'child_user_withdraw_at' => Carbon::now(),//时间
                                    'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                    'remark' => '三级高级团员提现一级团员抽佣比例' . $group_cash,
                                ];
                                Commission::query()->create($commissionAttributes);
                                UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);

                                $group_cash = $group['commissions3_3'];//
                                $amount = $userWithdraw->amount;//提现金币
                                $commissionAttributes = [
                                    'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                    'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                    'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                    'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                    'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                    'exchange_rate' => $exchangeRate,//兑换比例
                                    'amount' => $amount * $group_cash,//奖励金币数
                                    'prize' => $group_cash,//佣金比例
                                    'child_user_withdraw_at' => Carbon::now(),//时间
                                    'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                    'remark' => '三级高级团员提现高级团长抽佣比例 ' . $group_cash,
                                ];
                                Commission::query()->create($commissionAttributes);
                                UserWalletService::income($parentUserthree, $amount, Enum::WALLET_TYPE_COMMISSION);

                            }
                        }
                    }
                }
            }
            //师徒奖励发放
            self::byNormal($userWithdraw);

            DB::commit();
        } catch (\Exception $exception) {
            Log::channel('invites')->error($exception->getMessage());
            DB::rollBack();
        }


    }

    public static function distribute_42(UserWithdraw $userWithdraw)
    {
        $group = Setting::get('group');


        //先判断用户是否已经发放过佣金
        $Commission = Commission::query()
            ->where([
                'child_user_withdraw_id' => $userWithdraw->id,
            ])
            ->first();

//        if (!empty($Commission)) {
//            Log::error('佣金已经发放过' . $Commission->id);
//            return;
//        }
//提现用户
        /** @var User $childUser */
        $childUser = $userWithdraw->user;


//提现用户的上1级用户
        /** @var User $parentUser */
        $parentUser = $childUser->parent;


        // 无上1级
        if (empty($parentUser)) {
            return;
        }
        //当前提现金币数

        DB::beginTransaction();

        //提现用户的上级用户的上级用户  等于  提现用户
        if ($parentUser->parent_id == $childUser->id) {
            Log::error(sprintf('无效用户对应关系Users:(%s, %s)', $childUser->id, $parentUser->parent_id));
            return;
        }
        $exchangeRate = Setting::get('exchange_rate');
        //  DB::beginTransaction();
        try {
            if ($childUser->is_advanced_group == 1) {
//上级是高级团长
                if ($parentUser->is_advanced_group == 1) {
                    //当前提现用户是 高级团长，上级 高级团长拿26，
                    $group_cash = $group['commissions7_26'];//
                    $amount = $userWithdraw->amount;//提现金币
                    $commissionAttributes = [
                        'user_id' => $parentUser->getAttribute('id'),//上级用户id
                        'child_user_id' => $childUser->getAttribute('id'),//提现用户
                        'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                        'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                        'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                        'exchange_rate' => $exchangeRate,//兑换比例
                        'amount' => $amount * $group_cash,//奖励金币数
                        'prize' => $group_cash,//佣金比例
                        'child_user_withdraw_at' => Carbon::now(),//时间
                        'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                        'remark' => '高级团长直推高级团长' . $group_cash,
                    ];
                    Commission::query()->create($commissionAttributes);
                    UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);
                } else if ($parentUser->is_group_leader == 1) {
                    //当前提现用户是 高级团长，上级 普通团长拿9，上级 高级团长拿26，
                    $group_cash = $group['commissions7_9'];//
                    $amount = $userWithdraw->amount;//提现金币
                    $commissionAttributes = [
                        'user_id' => $parentUser->getAttribute('id'),//上级用户id
                        'child_user_id' => $childUser->getAttribute('id'),//提现用户
                        'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                        'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                        'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                        'exchange_rate' => $exchangeRate,//兑换比例
                        'amount' => $amount * $group_cash,//奖励金币数
                        'prize' => $group_cash,//佣金比例
                        'child_user_withdraw_at' => Carbon::now(),//时间
                        'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                        'remark' => '普通团长直推高级团长收益比例' . $group_cash,
                    ];
                    Commission::query()->create($commissionAttributes);
                    UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);
                } else {
                    //判断上2级
                    $parentUsertwo = $parentUser->parent;
                    if (!empty($parentUsertwo)) {

                        //上2级是团长
                        if ($parentUsertwo->is_advanced_group == 1) {

                            //当前提现用户是高级团长，上级高级1级团员拿32，上上级 ，高级团长拿28，
                            $group_cash = $group['commissions7_32'];//
                            $amount = $userWithdraw->amount;//提现金币
                            $commissionAttributes = [
                                'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                'exchange_rate' => $exchangeRate,//兑换比例
                                'amount' => $amount * $group_cash,//奖励金币数
                                'prize' => $group_cash,//佣金比例
                                'child_user_withdraw_at' => Carbon::now(),//时间
                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                'remark' => '高级团员1级直推高级团长' . $group_cash,
                            ];
                            Commission::query()->create($commissionAttributes);
                            UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                            $group_cash = $group['commissions7_28'];//
                            $amount = $userWithdraw->amount;//提现金币
                            $commissionAttributes = [
                                'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                'exchange_rate' => $exchangeRate,//兑换比例
                                'amount' => $amount * $group_cash,//奖励金币数
                                'prize' => $group_cash,//佣金比例
                                'child_user_withdraw_at' => Carbon::now(),//时间
                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                'remark' => '高级团长间推高级团长' . $group_cash,
                            ];
                            Commission::query()->create($commissionAttributes);
                            UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);

                        } else if ($parentUsertwo->is_group_leader == 1) {

                            //当前提现用户是高级团长，上级普通1级团员拿15，上上级 ，普通团长拿12，

                            $group_cash = $group['commissions7_15'];//
                            $amount = $userWithdraw->amount;//提现金币
                            $commissionAttributes = [
                                'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                'exchange_rate' => $exchangeRate,//兑换比例
                                'amount' => $amount * $group_cash,//奖励金币数
                                'prize' => $group_cash,//佣金比例
                                'child_user_withdraw_at' => Carbon::now(),//时间
                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                'remark' => '普通团员1级直推高级团长收益比例' . $group_cash,
                            ];
                            Commission::query()->create($commissionAttributes);
                            UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                            $group_cash = $group['commissions7_12'];//
                            $amount = $userWithdraw->amount;//提现金币
                            $commissionAttributes = [
                                'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                'exchange_rate' => $exchangeRate,//兑换比例
                                'amount' => $amount * $group_cash,//奖励金币数
                                'prize' => $group_cash,//佣金比例
                                'child_user_withdraw_at' => Carbon::now(),//时间
                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                'remark' => '普通团长间推高级团长收益比例' . $group_cash,
                            ];
                            Commission::query()->create($commissionAttributes);
                            UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);


                        } else {

                            //判断上3级

                            $parentUserthree = $parentUsertwo->parent;
                            if (!empty($parentUserthree)) {
                                if ($parentUserthree->is_advanced_group == 1) {

                                    //当前提现用户是高级团长，上级高级2级团员拿38，上上级，高级1级团员拿36，

                                    $group_cash = $group['commissions7_38'];//
                                    $amount = $userWithdraw->amount;//提现金币
                                    $commissionAttributes = [
                                        'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                        'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                        'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                        'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                        'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                        'exchange_rate' => $exchangeRate,//兑换比例
                                        'amount' => $amount * $group_cash,//奖励金币数
                                        'prize' => $group_cash,//佣金比例
                                        'child_user_withdraw_at' => Carbon::now(),//时间
                                        'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                        'remark' => '高级团员2级直推高级团长' . $group_cash,
                                    ];
                                    Commission::query()->create($commissionAttributes);
                                    UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                    $group_cash = $group['commissions7_36'];//
                                    $amount = $userWithdraw->amount;//提现金币
                                    $commissionAttributes = [
                                        'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                        'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                        'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                        'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                        'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                        'exchange_rate' => $exchangeRate,//兑换比例
                                        'amount' => $amount * $group_cash,//奖励金币数
                                        'prize' => $group_cash,//佣金比例
                                        'child_user_withdraw_at' => Carbon::now(),//时间
                                        'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                        'remark' => '高级团员1级间推高级团长' . $group_cash,
                                    ];
                                    Commission::query()->create($commissionAttributes);
                                    UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);

                                } else if ($parentUserthree->is_group_leader == 1) {

                                    //当前提现用户是高级团长，上级普通2级团员拿21，上上级，普通1级团员拿18，

                                    $group_cash = $group['commissions7_21'];//
                                    $amount = $userWithdraw->amount;//提现金币
                                    $commissionAttributes = [
                                        'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                        'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                        'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                        'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                        'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                        'exchange_rate' => $exchangeRate,//兑换比例
                                        'amount' => $amount * $group_cash,//奖励金币数
                                        'prize' => $group_cash,//佣金比例
                                        'child_user_withdraw_at' => Carbon::now(),//时间
                                        'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                        'remark' => '普通团员2级直推高级团长收益比例' . $group_cash,
                                    ];
                                    Commission::query()->create($commissionAttributes);
                                    UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                    $group_cash = $group['commissions7_18'];//
                                    $amount = $userWithdraw->amount;//提现金币
                                    $commissionAttributes = [
                                        'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                        'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                        'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                        'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                        'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                        'exchange_rate' => $exchangeRate,//兑换比例
                                        'amount' => $amount * $group_cash,//奖励金币数
                                        'prize' => $group_cash,//佣金比例
                                        'child_user_withdraw_at' => Carbon::now(),//时间
                                        'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                        'remark' => '普通团员1级间推高级团长收益比例' . $group_cash,
                                    ];
                                    Commission::query()->create($commissionAttributes);
                                    UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);


                                } else {

                                    //上4级
                                    $parentUserfour = $parentUserthree->parent;
                                    if (!empty($parentUserfour)) {
                                        if ($parentUserfour->is_advanced_group == 1) {
                                            //上4级是团长
                                            //当前提现用户是高级团长，上级是普通拿3，上上级是高级2级团员拿41，
                                            $group_cash = $group['commissions7_3'];//
                                            $amount = $userWithdraw->amount;//提现金币
                                            $commissionAttributes = [
                                                'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                                'exchange_rate' => $exchangeRate,//兑换比例
                                                'amount' => $amount * $group_cash,//奖励金币数
                                                'prize' => $group_cash,//佣金比例
                                                'child_user_withdraw_at' => Carbon::now(),//时间
                                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                                'remark' => '普通用户直推高级团长收益比例' . $group_cash,
                                            ];
                                            Commission::query()->create($commissionAttributes);
                                            UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                            $group_cash = $group['commissions7_41'];//
                                            $amount = $userWithdraw->amount;//提现金币
                                            $commissionAttributes = [
                                                'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                                'exchange_rate' => $exchangeRate,//兑换比例
                                                'amount' => $amount * $group_cash,//奖励金币数
                                                'prize' => $group_cash,//佣金比例
                                                'child_user_withdraw_at' => Carbon::now(),//时间
                                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                                'remark' => '高级团员2级间推高级团长' . $group_cash,
                                            ];
                                            Commission::query()->create($commissionAttributes);
                                            UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);


                                        } else if ($parentUserfour->is_group_leader == 1) {
                                            //上4级是团长
                                            //当前提现用户是高级团长，上级是普通拿3，上上级是2级团员拿24


                                            $group_cash = $group['commissions7_3'];//
                                            $amount = $userWithdraw->amount;//提现金币
                                            $commissionAttributes = [
                                                'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                                'exchange_rate' => $exchangeRate,//兑换比例
                                                'amount' => $amount * $group_cash,//奖励金币数
                                                'prize' => $group_cash,//佣金比例
                                                'child_user_withdraw_at' => Carbon::now(),//时间
                                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                                'remark' => '普通用户直推高级团长收益比例' . $group_cash,
                                            ];
                                            Commission::query()->create($commissionAttributes);
                                            UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                            $group_cash = $group['commissions7_24'];//
                                            $amount = $userWithdraw->amount;//提现金币
                                            $commissionAttributes = [
                                                'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                                'exchange_rate' => $exchangeRate,//兑换比例
                                                'amount' => $amount * $group_cash,//奖励金币数
                                                'prize' => $group_cash,//佣金比例
                                                'child_user_withdraw_at' => Carbon::now(),//时间
                                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                                'remark' => '普通团员2级间推高级团长收益比例' . $group_cash,
                                            ];
                                            Commission::query()->create($commissionAttributes);
                                            UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);


                                        } else {
//上5级

                                            //发放  提现用户是高级团长提现，上一级 用户拿3 ，上上级 用户拿6，
                                            $group_cash = $group['commissions7_3'];//
                                            $amount = $userWithdraw->amount;//提现金币
                                            $commissionAttributes = [
                                                'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                                'exchange_rate' => $exchangeRate,//兑换比例
                                                'amount' => $amount * $group_cash,//奖励金币数
                                                'prize' => $group_cash,//佣金比例
                                                'child_user_withdraw_at' => Carbon::now(),//时间
                                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                                'remark' => '普通用户直推高级团长收益比例' . $group_cash,
                                            ];
                                            Commission::query()->create($commissionAttributes);
                                            UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                            $group_cash = $group['commissions7_6'];//
                                            $amount = $userWithdraw->amount;//提现金币
                                            $commissionAttributes = [
                                                'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                                'exchange_rate' => $exchangeRate,//兑换比例
                                                'amount' => $amount * $group_cash,//奖励金币数
                                                'prize' => $group_cash,//佣金比例
                                                'child_user_withdraw_at' => Carbon::now(),//时间
                                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                                'remark' => '普通用户间推高级团长收益比例' . $group_cash,
                                            ];
                                            Commission::query()->create($commissionAttributes);
                                            UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);

                                        }


                                    } else {
                                        //发放    //当前提现用户是高级团长，上一级 用户拿3 ，上上级 用户拿6，
                                        $group_cash = $group['commissions7_3'];//
                                        $amount = $userWithdraw->amount;//提现金币
                                        $commissionAttributes = [
                                            'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                            'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                            'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                            'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                            'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                            'exchange_rate' => $exchangeRate,//兑换比例
                                            'amount' => $amount * $group_cash,//奖励金币数
                                            'prize' => $group_cash,//佣金比例
                                            'child_user_withdraw_at' => Carbon::now(),//时间
                                            'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                            'remark' => '普通用户直推高级团长收益比例' . $group_cash,
                                        ];
                                        Commission::query()->create($commissionAttributes);
                                        UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                        $group_cash = $group['commissions7_6'];//
                                        $amount = $userWithdraw->amount;//提现金币
                                        $commissionAttributes = [
                                            'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                            'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                            'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                            'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                            'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                            'exchange_rate' => $exchangeRate,//兑换比例
                                            'amount' => $amount * $group_cash,//奖励金币数
                                            'prize' => $group_cash,//佣金比例
                                            'child_user_withdraw_at' => Carbon::now(),//时间
                                            'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                            'remark' => '普通用户间推高级团长收益比例' . $group_cash,
                                        ];
                                        Commission::query()->create($commissionAttributes);
                                        UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);

                                    }


                                }


                            } else {

                                //发放    //当前提现用户是高级团长，上一级 用户拿3 ，上上级 用户拿6，
                                $group_cash = $group['commissions7_3'];//
                                $amount = $userWithdraw->amount;//提现金币
                                $commissionAttributes = [
                                    'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                    'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                    'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                    'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                    'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                    'exchange_rate' => $exchangeRate,//兑换比例
                                    'amount' => $amount * $group_cash,//奖励金币数
                                    'prize' => $group_cash,//佣金比例
                                    'child_user_withdraw_at' => Carbon::now(),//时间
                                    'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                    'remark' => '普通用户直推高级团长收益比例' . $group_cash,
                                ];
                                Commission::query()->create($commissionAttributes);
                                UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                $group_cash = $group['commissions7_6'];//
                                $amount = $userWithdraw->amount;//提现金币
                                $commissionAttributes = [
                                    'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                    'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                    'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                    'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                    'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                    'exchange_rate' => $exchangeRate,//兑换比例
                                    'amount' => $amount * $group_cash,//奖励金币数
                                    'prize' => $group_cash,//佣金比例
                                    'child_user_withdraw_at' => Carbon::now(),//时间
                                    'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                    'remark' => '普通用户间推高级团长收益比例' . $group_cash,
                                ];
                                Commission::query()->create($commissionAttributes);
                                UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);

                            }


                        }

                    } else {
                        //当前提现用户是高级团长，上级 是普通拿3，无上上
                        $group_cash = $group['commissions7_3'];//
                        $amount = $userWithdraw->amount;//提现金币
                        $commissionAttributes = [
                            'user_id' => $parentUser->getAttribute('id'),//上级用户id
                            'child_user_id' => $childUser->getAttribute('id'),//提现用户
                            'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                            'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                            'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                            'exchange_rate' => $exchangeRate,//兑换比例
                            'amount' => $amount * $group_cash,//奖励金币数
                            'prize' => $group_cash,//佣金比例
                            'child_user_withdraw_at' => Carbon::now(),//时间
                            'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                            'remark' => '普通用户直推高级团长收益比例' . $group_cash,
                        ];
                        Commission::query()->create($commissionAttributes);
                        UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                    }

                }


            } else if ($childUser->is_group_leader == 1) {
                //当前提现用户是普通团长
                if ($parentUser->is_advanced_group == 1) {

                    //当前提现用户是普通团长，上级 高级团长拿25，
                    $group_cash = $group['commissions7_25'];//
                    $amount = $userWithdraw->amount;//提现金币
                    $commissionAttributes = [
                        'user_id' => $parentUser->getAttribute('id'),//上级用户id
                        'child_user_id' => $childUser->getAttribute('id'),//提现用户
                        'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                        'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                        'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                        'exchange_rate' => $exchangeRate,//兑换比例
                        'amount' => $amount * $group_cash,//奖励金币数
                        'prize' => $group_cash,//佣金比例
                        'child_user_withdraw_at' => Carbon::now(),//时间
                        'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                        'remark' => '高级团长直推普通团长' . $group_cash,
                    ];
                    Commission::query()->create($commissionAttributes);
                    UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);
                } else if ($parentUser->is_group_leader == 1) {

                    //当前提现用户是普通团长，上级 普通团长拿8
                    $group_cash = $group['commissions7_8'];//
                    $amount = $userWithdraw->amount;//提现金币
                    $commissionAttributes = [
                        'user_id' => $parentUser->getAttribute('id'),//上级用户id
                        'child_user_id' => $childUser->getAttribute('id'),//提现用户
                        'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                        'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                        'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                        'exchange_rate' => $exchangeRate,//兑换比例
                        'amount' => $amount * $group_cash,//奖励金币数
                        'prize' => $group_cash,//佣金比例
                        'child_user_withdraw_at' => Carbon::now(),//时间
                        'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                        'remark' => '普通团长直推普通团长收益比例' . $group_cash,
                    ];
                    Commission::query()->create($commissionAttributes);
                    UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                } else {
                    //判断上2级
                    $parentUsertwo = $parentUser->parent;
                    if (!empty($parentUsertwo)) {

                        //上2级是普通团长
                        if ($parentUsertwo->is_advanced_group == 1) {

                            //当前提现用户是普通团长，上级高级1级团员拿31，上上级 ，高级团长拿29，
                            $group_cash = $group['commissions7_31'];//
                            $amount = $userWithdraw->amount;//提现金币
                            $commissionAttributes = [
                                'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                'exchange_rate' => $exchangeRate,//兑换比例
                                'amount' => $amount * $group_cash,//奖励金币数
                                'prize' => $group_cash,//佣金比例
                                'child_user_withdraw_at' => Carbon::now(),//时间
                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                'remark' => '高级团员1级直推普通团长' . $group_cash,
                            ];
                            Commission::query()->create($commissionAttributes);
                            UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                            $group_cash = $group['commissions7_29'];//
                            $amount = $userWithdraw->amount;//提现金币
                            $commissionAttributes = [
                                'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                'exchange_rate' => $exchangeRate,//兑换比例
                                'amount' => $amount * $group_cash,//奖励金币数
                                'prize' => $group_cash,//佣金比例
                                'child_user_withdraw_at' => Carbon::now(),//时间
                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                'remark' => '高级团长间推普通团长' . $group_cash,
                            ];
                            Commission::query()->create($commissionAttributes);
                            UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);

                        } else if ($parentUsertwo->is_group_leader == 1) {
                            //当前提现用户是普通团长，上级普通1级团员拿14，上上级 ，普通团长拿11，

                            $group_cash = $group['commissions7_14'];//
                            $amount = $userWithdraw->amount;//提现金币
                            $commissionAttributes = [
                                'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                'exchange_rate' => $exchangeRate,//兑换比例
                                'amount' => $amount * $group_cash,//奖励金币数
                                'prize' => $group_cash,//佣金比例
                                'child_user_withdraw_at' => Carbon::now(),//时间
                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                'remark' => '普通团员1级直推普通团长收益比例' . $group_cash,
                            ];
                            Commission::query()->create($commissionAttributes);
                            UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                            $group_cash = $group['commissions7_11'];//
                            $amount = $userWithdraw->amount;//提现金币
                            $commissionAttributes = [
                                'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                'exchange_rate' => $exchangeRate,//兑换比例
                                'amount' => $amount * $group_cash,//奖励金币数
                                'prize' => $group_cash,//佣金比例
                                'child_user_withdraw_at' => Carbon::now(),//时间
                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                'remark' => '普通团长间推普通团长收益比例' . $group_cash,
                            ];
                            Commission::query()->create($commissionAttributes);
                            UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);

                        } else {

                            //判断上3级

                            $parentUserthree = $parentUsertwo->parent;
                            if (!empty($parentUserthree)) {
                                if ($parentUserthree->is_advanced_group == 1) {
                                    //当前提现用户是普通团长， 上级高级2级团员拿39，上上级，高级1级团员拿35，

                                    $group_cash = $group['commissions7_39'];//
                                    $amount = $userWithdraw->amount;//提现金币
                                    $commissionAttributes = [
                                        'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                        'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                        'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                        'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                        'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                        'exchange_rate' => $exchangeRate,//兑换比例
                                        'amount' => $amount * $group_cash,//奖励金币数
                                        'prize' => $group_cash,//佣金比例
                                        'child_user_withdraw_at' => Carbon::now(),//时间
                                        'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                        'remark' => '高级团员2级直推普通团长' . $group_cash,
                                    ];
                                    Commission::query()->create($commissionAttributes);
                                    UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                    $group_cash = $group['commissions7_35'];//
                                    $amount = $userWithdraw->amount;//提现金币
                                    $commissionAttributes = [
                                        'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                        'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                        'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                        'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                        'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                        'exchange_rate' => $exchangeRate,//兑换比例
                                        'amount' => $amount * $group_cash,//奖励金币数
                                        'prize' => $group_cash,//佣金比例
                                        'child_user_withdraw_at' => Carbon::now(),//时间
                                        'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                        'remark' => '高级团员1级间推普通团长' . $group_cash,
                                    ];
                                    Commission::query()->create($commissionAttributes);
                                    UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);

                                } else if ($parentUserthree->is_group_leader == 1) {
                                    //当前提现用户是普通团长，上级普通2级团员拿20，上上级，普通1级团员拿17，

                                    $group_cash = $group['commissions7_20'];//
                                    $amount = $userWithdraw->amount;//提现金币
                                    $commissionAttributes = [
                                        'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                        'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                        'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                        'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                        'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                        'exchange_rate' => $exchangeRate,//兑换比例
                                        'amount' => $amount * $group_cash,//奖励金币数
                                        'prize' => $group_cash,//佣金比例
                                        'child_user_withdraw_at' => Carbon::now(),//时间
                                        'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                        'remark' => '普通团员2级直推普通团长收益比例' . $group_cash,
                                    ];
                                    Commission::query()->create($commissionAttributes);
                                    UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                    $group_cash = $group['commissions7_17'];//
                                    $amount = $userWithdraw->amount;//提现金币
                                    $commissionAttributes = [
                                        'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                        'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                        'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                        'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                        'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                        'exchange_rate' => $exchangeRate,//兑换比例
                                        'amount' => $amount * $group_cash,//奖励金币数
                                        'prize' => $group_cash,//佣金比例
                                        'child_user_withdraw_at' => Carbon::now(),//时间
                                        'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                        'remark' => '普通团员1级间推普通团长收益比例' . $group_cash,
                                    ];
                                    Commission::query()->create($commissionAttributes);
                                    UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);

                                } else {

                                    //上4级
                                    $parentUserfour = $parentUserthree->parent;
                                    if (!empty($parentUserfour)) {
                                        if ($parentUserfour->is_advanced_group == 1) {
                                            //上4级是团长

                                            //当前提现用户是普通团长，上级是普通拿2，高级2级团员拿42，
                                            $group_cash = $group['commissions7_2'];//
                                            $amount = $userWithdraw->amount;//提现金币
                                            $commissionAttributes = [
                                                'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                                'exchange_rate' => $exchangeRate,//兑换比例
                                                'amount' => $amount * $group_cash,//奖励金币数
                                                'prize' => $group_cash,//佣金比例
                                                'child_user_withdraw_at' => Carbon::now(),//时间
                                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                                'remark' => '普通用户直推普通团长收益比例' . $group_cash,
                                            ];
                                            Commission::query()->create($commissionAttributes);
                                            UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                            $group_cash = $group['commissions7_42'];//
                                            $amount = $userWithdraw->amount;//提现金币
                                            $commissionAttributes = [
                                                'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                                'exchange_rate' => $exchangeRate,//兑换比例
                                                'amount' => $amount * $group_cash,//奖励金币数
                                                'prize' => $group_cash,//佣金比例
                                                'child_user_withdraw_at' => Carbon::now(),//时间
                                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                                'remark' => '高级团员2级间推普通团长' . $group_cash,
                                            ];
                                            Commission::query()->create($commissionAttributes);
                                            UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);

                                        } else if ($parentUserfour->is_group_leader == 1) {
                                            //上4级是团长

                                            //当前提现用户是普通团长，上级是普通拿2，上上级是普通2级团员拿23，
                                            $group_cash = $group['commissions7_2'];//
                                            $amount = $userWithdraw->amount;//提现金币
                                            $commissionAttributes = [
                                                'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                                'exchange_rate' => $exchangeRate,//兑换比例
                                                'amount' => $amount * $group_cash,//奖励金币数
                                                'prize' => $group_cash,//佣金比例
                                                'child_user_withdraw_at' => Carbon::now(),//时间
                                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                                'remark' => '普通用户直推普通团长收益比例' . $group_cash,
                                            ];
                                            Commission::query()->create($commissionAttributes);
                                            UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                            $group_cash = $group['commissions7_23'];//
                                            $amount = $userWithdraw->amount;//提现金币
                                            $commissionAttributes = [
                                                'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                                'exchange_rate' => $exchangeRate,//兑换比例
                                                'amount' => $amount * $group_cash,//奖励金币数
                                                'prize' => $group_cash,//佣金比例
                                                'child_user_withdraw_at' => Carbon::now(),//时间
                                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                                'remark' => '普通团员2级间推普通团长收益比例' . $group_cash,
                                            ];
                                            Commission::query()->create($commissionAttributes);
                                            UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);

                                        } else {
//上5级
                                            //发放  提现用户是普通团长提现，上一级 用户拿2 ，上上级 用户拿5，

                                            $group_cash = $group['commissions7_2'];//
                                            $amount = $userWithdraw->amount;//提现金币
                                            $commissionAttributes = [
                                                'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                                'exchange_rate' => $exchangeRate,//兑换比例
                                                'amount' => $amount * $group_cash,//奖励金币数
                                                'prize' => $group_cash,//佣金比例
                                                'child_user_withdraw_at' => Carbon::now(),//时间
                                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                                'remark' => '普通用户直推普通团长收益比例' . $group_cash,
                                            ];
                                            Commission::query()->create($commissionAttributes);
                                            UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                            $group_cash = $group['commissions7_5'];//
                                            $amount = $userWithdraw->amount;//提现金币
                                            $commissionAttributes = [
                                                'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                                'exchange_rate' => $exchangeRate,//兑换比例
                                                'amount' => $amount * $group_cash,//奖励金币数
                                                'prize' => $group_cash,//佣金比例
                                                'child_user_withdraw_at' => Carbon::now(),//时间
                                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                                'remark' => '普通用户间推普通团长收益比例' . $group_cash,
                                            ];
                                            Commission::query()->create($commissionAttributes);
                                            UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);
                                        }

                                    } else {
                                        //发放  提现用户是普通团长，上一级 用户拿2 ，上上级 用户拿5，

                                        $group_cash = $group['commissions7_2'];//
                                        $amount = $userWithdraw->amount;//提现金币
                                        $commissionAttributes = [
                                            'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                            'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                            'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                            'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                            'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                            'exchange_rate' => $exchangeRate,//兑换比例
                                            'amount' => $amount * $group_cash,//奖励金币数
                                            'prize' => $group_cash,//佣金比例
                                            'child_user_withdraw_at' => Carbon::now(),//时间
                                            'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                            'remark' => '普通用户直推普通团长收益比例' . $group_cash,
                                        ];
                                        Commission::query()->create($commissionAttributes);
                                        UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                        $group_cash = $group['commissions7_5'];//
                                        $amount = $userWithdraw->amount;//提现金币
                                        $commissionAttributes = [
                                            'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                            'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                            'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                            'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                            'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                            'exchange_rate' => $exchangeRate,//兑换比例
                                            'amount' => $amount * $group_cash,//奖励金币数
                                            'prize' => $group_cash,//佣金比例
                                            'child_user_withdraw_at' => Carbon::now(),//时间
                                            'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                            'remark' => '普通用户间推普通团长收益比例' . $group_cash,
                                        ];
                                        Commission::query()->create($commissionAttributes);
                                        UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);

                                    }
                                }

                            } else {

                                //当前提现用户是团长，上级 用户拿2，上上级用户拿5，

                                $group_cash = $group['commissions7_2'];//
                                $amount = $userWithdraw->amount;//提现金币
                                $commissionAttributes = [
                                    'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                    'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                    'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                    'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                    'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                    'exchange_rate' => $exchangeRate,//兑换比例
                                    'amount' => $amount * $group_cash,//奖励金币数
                                    'prize' => $group_cash,//佣金比例
                                    'child_user_withdraw_at' => Carbon::now(),//时间
                                    'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                    'remark' => '普通用户直推普通团长收益比例' . $group_cash,
                                ];
                                Commission::query()->create($commissionAttributes);
                                UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                $group_cash = $group['commissions7_5'];//
                                $amount = $userWithdraw->amount;//提现金币
                                $commissionAttributes = [
                                    'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                    'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                    'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                    'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                    'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                    'exchange_rate' => $exchangeRate,//兑换比例
                                    'amount' => $amount * $group_cash,//奖励金币数
                                    'prize' => $group_cash,//佣金比例
                                    'child_user_withdraw_at' => Carbon::now(),//时间
                                    'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                    'remark' => '普通用户间推普通团长收益比例' . $group_cash,
                                ];
                                Commission::query()->create($commissionAttributes);
                                UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);

                            }

                        }

                    } else {
                        //当前提现用户是团长，上级 是普通拿2，无上上
                        $group_cash = $group['commissions7_2'];//
                        $amount = $userWithdraw->amount;//提现金币
                        $commissionAttributes = [
                            'user_id' => $parentUser->getAttribute('id'),//上级用户id
                            'child_user_id' => $childUser->getAttribute('id'),//提现用户
                            'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                            'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                            'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                            'exchange_rate' => $exchangeRate,//兑换比例
                            'amount' => $amount * $group_cash,//奖励金币数
                            'prize' => $group_cash,//佣金比例
                            'child_user_withdraw_at' => Carbon::now(),//时间
                            'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                            'remark' => '普通用户直推普通团长收益比例' . $group_cash,
                        ];
                        Commission::query()->create($commissionAttributes);
                        UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                    }

                }


            } else {
                if ($parentUser->is_advanced_group == 1) {
                    //当前不是团长
                    //上级 高级团长拿27

                    $group_cash = $group['commissions7_27'];//
                    $amount = $userWithdraw->amount;//提现金币
                    $commissionAttributes = [
                        'user_id' => $parentUser->getAttribute('id'),//上级用户id
                        'child_user_id' => $childUser->getAttribute('id'),//提现用户
                        'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                        'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                        'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                        'exchange_rate' => $exchangeRate,//兑换比例
                        'amount' => $amount * $group_cash,//奖励金币数
                        'prize' => $group_cash,//佣金比例
                        'child_user_withdraw_at' => Carbon::now(),//时间
                        'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                        'remark' => '高级团长直推高级团员1级' . $group_cash,
                    ];
                    Commission::query()->create($commissionAttributes);
                    UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);


                } else if ($parentUser->is_group_leader == 1) {
                    //当前不是团长
                    //上级 普通团长拿7

                    $group_cash = $group['commissions7_7'];//
                    $amount = $userWithdraw->amount;//提现金币
                    $commissionAttributes = [
                        'user_id' => $parentUser->getAttribute('id'),//上级用户id
                        'child_user_id' => $childUser->getAttribute('id'),//提现用户
                        'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                        'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                        'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                        'exchange_rate' => $exchangeRate,//兑换比例
                        'amount' => $amount * $group_cash,//奖励金币数
                        'prize' => $group_cash,//佣金比例
                        'child_user_withdraw_at' => Carbon::now(),//时间
                        'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                        'remark' => '普通团长直推普通团员1级收益比例' . $group_cash,
                    ];
                    Commission::query()->create($commissionAttributes);
                    UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);


                } else {
                    //用户可能是普通用户或者2级团员

                    //判断上2级
                    $parentUsertwo = $parentUser->parent;

                    if (!empty($parentUsertwo)) {

                        //上2级是团长
                        if ($parentUsertwo->is_advanced_group == 1) {


                            //提现用户2级团员  上级 高级1级团员拿33，上上级高级团长拿30，
                            $group_cash = $group['commissions7_33'];//
                            $amount = $userWithdraw->amount;//提现金币
                            $commissionAttributes = [
                                'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                'exchange_rate' => $exchangeRate,//兑换比例
                                'amount' => $amount * $group_cash,//奖励金币数
                                'prize' => $group_cash,//佣金比例
                                'child_user_withdraw_at' => Carbon::now(),//时间
                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                'remark' => '高级团员1级直推高级团员2级' . $group_cash,
                            ];
                            Commission::query()->create($commissionAttributes);
                            UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                            $group_cash = $group['commissions7_30'];//
                            $amount = $userWithdraw->amount;//提现金币
                            $commissionAttributes = [
                                'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                'exchange_rate' => $exchangeRate,//兑换比例
                                'amount' => $amount * $group_cash,//奖励金币数
                                'prize' => $group_cash,//佣金比例
                                'child_user_withdraw_at' => Carbon::now(),//时间
                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                'remark' => '高级团长间推高级团员2级' . $group_cash,
                            ];
                            Commission::query()->create($commissionAttributes);
                            UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);


                        } else if ($parentUsertwo->is_group_leader == 1) {

                            //提现用户2级团员  上级 普通1级团员拿13，上上级 普通团长拿10，
                            $group_cash = $group['commissions7_13'];//
                            $amount = $userWithdraw->amount;//提现金币
                            $commissionAttributes = [
                                'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                'exchange_rate' => $exchangeRate,//兑换比例
                                'amount' => $amount * $group_cash,//奖励金币数
                                'prize' => $group_cash,//佣金比例
                                'child_user_withdraw_at' => Carbon::now(),//时间
                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                'remark' => '普通团员1级直推普通团员2级收益比例' . $group_cash,
                            ];
                            Commission::query()->create($commissionAttributes);
                            UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                            $group_cash = $group['commissions7_10'];//
                            $amount = $userWithdraw->amount;//提现金币
                            $commissionAttributes = [
                                'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                'exchange_rate' => $exchangeRate,//兑换比例
                                'amount' => $amount * $group_cash,//奖励金币数
                                'prize' => $group_cash,//佣金比例
                                'child_user_withdraw_at' => Carbon::now(),//时间
                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                'remark' => '普通团长间推普通团员2级收益比例' . $group_cash,
                            ];
                            Commission::query()->create($commissionAttributes);
                            UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);


                        } else {

                            //判断上3级

                            $parentUserthree = $parentUsertwo->parent;
                            if (!empty($parentUserthree)) {
                                if ($parentUserthree->is_advanced_group == 1) {
                                    //普通用户提现         //上一级 高级2级团员拿37，上上级 高级1级团员拿34，
                                    $group_cash = $group['commissions7_37'];//
                                    $amount = $userWithdraw->amount;//提现金币
                                    $commissionAttributes = [
                                        'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                        'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                        'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                        'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                        'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                        'exchange_rate' => $exchangeRate,//兑换比例
                                        'amount' => $amount * $group_cash,//奖励金币数
                                        'prize' => $group_cash,//佣金比例
                                        'child_user_withdraw_at' => Carbon::now(),//时间
                                        'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                        'remark' => '高级团员2级直推用户' . $group_cash,
                                    ];
                                    Commission::query()->create($commissionAttributes);
                                    UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                    $group_cash = $group['commissions7_34'];//
                                    $amount = $userWithdraw->amount;//提现金币
                                    $commissionAttributes = [
                                        'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                        'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                        'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                        'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                        'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                        'exchange_rate' => $exchangeRate,//兑换比例
                                        'amount' => $amount * $group_cash,//奖励金币数
                                        'prize' => $group_cash,//佣金比例
                                        'child_user_withdraw_at' => Carbon::now(),//时间
                                        'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                        'remark' => '高级团员1级间推用户' . $group_cash,
                                    ];
                                    Commission::query()->create($commissionAttributes);
                                    UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);


                                } else if ($parentUserthree->is_group_leader == 1) {
                                    //普通用户提现  上一级 普通2级团员拿19，上上级 普通1级团员拿16，
                                    $group_cash = $group['commissions7_19'];//
                                    $amount = $userWithdraw->amount;//提现金币
                                    $commissionAttributes = [
                                        'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                        'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                        'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                        'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                        'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                        'exchange_rate' => $exchangeRate,//兑换比例
                                        'amount' => $amount * $group_cash,//奖励金币数
                                        'prize' => $group_cash,//佣金比例
                                        'child_user_withdraw_at' => Carbon::now(),//时间
                                        'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                        'remark' => '普通团员2级直推用户收益比例' . $group_cash,
                                    ];
                                    Commission::query()->create($commissionAttributes);
                                    UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                    $group_cash = $group['commissions7_16'];//
                                    $amount = $userWithdraw->amount;//提现金币
                                    $commissionAttributes = [
                                        'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                        'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                        'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                        'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                        'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                        'exchange_rate' => $exchangeRate,//兑换比例
                                        'amount' => $amount * $group_cash,//奖励金币数
                                        'prize' => $group_cash,//佣金比例
                                        'child_user_withdraw_at' => Carbon::now(),//时间
                                        'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                        'remark' => '普通团员1级间推用户收益比例' . $group_cash,
                                    ];
                                    Commission::query()->create($commissionAttributes);
                                    UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);


                                } else {
                                    //上4级
                                    $parentUserfour = $parentUserthree->parent;
                                    if (!empty($parentUserfour)) {
                                        if ($parentUserfour->is_advanced_group == 1) {
                                            //当前用户是普通商户，上级是普通拿1。上上级是2级团员拿22，高级2级团员拿40，

                                            $group_cash = $group['commissions7_1'];//
                                            $amount = $userWithdraw->amount;//提现金币
                                            $commissionAttributes = [
                                                'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                                'exchange_rate' => $exchangeRate,//兑换比例
                                                'amount' => $amount * $group_cash,//奖励金币数
                                                'prize' => $group_cash,//佣金比例
                                                'child_user_withdraw_at' => Carbon::now(),//时间
                                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                                'remark' => '普通用户直推用户收益比例' . $group_cash,
                                            ];
                                            Commission::query()->create($commissionAttributes);
                                            UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                            $group_cash = $group['commissions7_40'];//
                                            $amount = $userWithdraw->amount;//提现金币
                                            $commissionAttributes = [
                                                'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                                'exchange_rate' => $exchangeRate,//兑换比例
                                                'amount' => $amount * $group_cash,//奖励金币数
                                                'prize' => $group_cash,//佣金比例
                                                'child_user_withdraw_at' => Carbon::now(),//时间
                                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                                'remark' => '高级团员2级间推用户' . $group_cash,
                                            ];
                                            Commission::query()->create($commissionAttributes);
                                            UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);


                                        } else if ($parentUserfour->is_group_leader == 1) {
                                            //当前用户是普通商户，上级是普通拿1。上上级是2级团员拿22，高级2级团员拿40，

                                            $group_cash = $group['commissions7_1'];//
                                            $amount = $userWithdraw->amount;//提现金币
                                            $commissionAttributes = [
                                                'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                                'exchange_rate' => $exchangeRate,//兑换比例
                                                'amount' => $amount * $group_cash,//奖励金币数
                                                'prize' => $group_cash,//佣金比例
                                                'child_user_withdraw_at' => Carbon::now(),//时间
                                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                                'remark' => '普通用户直推用户收益比例' . $group_cash,
                                            ];
                                            Commission::query()->create($commissionAttributes);
                                            UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                            $group_cash = $group['commissions7_22'];//
                                            $amount = $userWithdraw->amount;//提现金币
                                            $commissionAttributes = [
                                                'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                                'exchange_rate' => $exchangeRate,//兑换比例
                                                'amount' => $amount * $group_cash,//奖励金币数
                                                'prize' => $group_cash,//佣金比例
                                                'child_user_withdraw_at' => Carbon::now(),//时间
                                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                                'remark' => '普通团员2级间推用户收益比例' . $group_cash,
                                            ];
                                            Commission::query()->create($commissionAttributes);
                                            UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);


                                        } else {
//上5级
                                            //当前用户是普通商户，上级是普通拿1，上上级是普通拿4，
                                            $group_cash = $group['commissions7_1'];//
                                            $amount = $userWithdraw->amount;//提现金币
                                            $commissionAttributes = [
                                                'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                                'exchange_rate' => $exchangeRate,//兑换比例
                                                'amount' => $amount * $group_cash,//奖励金币数
                                                'prize' => $group_cash,//佣金比例
                                                'child_user_withdraw_at' => Carbon::now(),//时间
                                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                                'remark' => '普通用户直推用户收益比例' . $group_cash,
                                            ];
                                            Commission::query()->create($commissionAttributes);
                                            UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                            $group_cash = $group['commissions7_4'];//
                                            $amount = $userWithdraw->amount;//提现金币
                                            $commissionAttributes = [
                                                'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                                'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                                'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                                'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                                'exchange_rate' => $exchangeRate,//兑换比例
                                                'amount' => $amount * $group_cash,//奖励金币数
                                                'prize' => $group_cash,//佣金比例
                                                'child_user_withdraw_at' => Carbon::now(),//时间
                                                'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                                'remark' => '普通用户间推用户收益比例' . $group_cash,
                                            ];
                                            Commission::query()->create($commissionAttributes);
                                            UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);


                                        }


                                    } else {
                                        //发放  提现用户是普通用户，上一级 用户拿1 ，上上级 用户拿4

                                        $group_cash = $group['commissions7_1'];//
                                        $amount = $userWithdraw->amount;//提现金币
                                        $commissionAttributes = [
                                            'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                            'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                            'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                            'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                            'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                            'exchange_rate' => $exchangeRate,//兑换比例
                                            'amount' => $amount * $group_cash,//奖励金币数
                                            'prize' => $group_cash,//佣金比例
                                            'child_user_withdraw_at' => Carbon::now(),//时间
                                            'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                            'remark' => '普通用户直推用户收益比例' . $group_cash,
                                        ];
                                        Commission::query()->create($commissionAttributes);
                                        UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                        $group_cash = $group['commissions7_4'];//
                                        $amount = $userWithdraw->amount;//提现金币
                                        $commissionAttributes = [
                                            'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                            'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                            'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                            'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                            'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                            'exchange_rate' => $exchangeRate,//兑换比例
                                            'amount' => $amount * $group_cash,//奖励金币数
                                            'prize' => $group_cash,//佣金比例
                                            'child_user_withdraw_at' => Carbon::now(),//时间
                                            'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                            'remark' => '普通用户间推用户收益比例' . $group_cash,
                                        ];
                                        Commission::query()->create($commissionAttributes);
                                        UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);

                                    }


                                }
                            } else {

                                //发放  提现用户是普通用户，上一级 用户拿1 ，上上级 用户拿4
                                $group_cash = $group['commissions7_1'];//
                                $amount = $userWithdraw->amount;//提现金币
                                $commissionAttributes = [
                                    'user_id' => $parentUser->getAttribute('id'),//上级用户id
                                    'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                    'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                    'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                    'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                    'exchange_rate' => $exchangeRate,//兑换比例
                                    'amount' => $amount * $group_cash,//奖励金币数
                                    'prize' => $group_cash,//佣金比例
                                    'child_user_withdraw_at' => Carbon::now(),//时间
                                    'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                    'remark' => '普通用户直推用户收益比例' . $group_cash,
                                ];
                                Commission::query()->create($commissionAttributes);
                                UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);

                                $group_cash = $group['commissions7_4'];//
                                $amount = $userWithdraw->amount;//提现金币
                                $commissionAttributes = [
                                    'user_id' => $parentUsertwo->getAttribute('id'),//上级用户id
                                    'child_user_id' => $childUser->getAttribute('id'),//提现用户
                                    'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                                    'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                                    'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                                    'exchange_rate' => $exchangeRate,//兑换比例
                                    'amount' => $amount * $group_cash,//奖励金币数
                                    'prize' => $group_cash,//佣金比例
                                    'child_user_withdraw_at' => Carbon::now(),//时间
                                    'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                                    'remark' => '普通用户间推用户收益比例' . $group_cash,
                                ];
                                Commission::query()->create($commissionAttributes);
                                UserWalletService::income($parentUsertwo, $amount, Enum::WALLET_TYPE_COMMISSION);


                            }


                        }


                    } else {
                        //发放  提现用户是普通用户，上一级 用户拿1
                        $group_cash = $group['commissions7_1'];//
                        $amount = $userWithdraw->amount;//提现金币
                        $commissionAttributes = [
                            'user_id' => $parentUser->getAttribute('id'),//上级用户id
                            'child_user_id' => $childUser->getAttribute('id'),//提现用户
                            'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),//提现记录id
                            'child_user_money' => $userWithdraw->money,//提现用户提现佣金
                            'user_money' => $userWithdraw->money * $group_cash,//上级获得佣金
                            'exchange_rate' => $exchangeRate,//兑换比例
                            'amount' => $amount * $group_cash,//奖励金币数
                            'prize' => $group_cash,//佣金比例
                            'child_user_withdraw_at' => Carbon::now(),//时间
                            'source' => CommissionEnum::SOURCE_GROUP_CASH,//奖励类型  团长返佣金
                            'remark' => '普通用户直推用户收益比例' . $group_cash,
                        ];
                        Commission::query()->create($commissionAttributes);
                        UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);


                    }


                }
            }


            //师徒奖励发放
            self::byNormal($userWithdraw);

            DB::commit();
        } catch (\Exception $exception) {
            Log::channel('invites')->error($exception->getMessage());
            DB::rollBack();
        }


    }

    public static function distributeSubMemberGroup(User $user)
    {
        // 对上级团长进行返佣金
        $groupSubRate = Setting::get('group_sub_rate');
        $group = Setting::get('group');
        $exchangeRate = Setting::get('exchange_rate');
        $price = $group['price'] ?? 0;

        if ($groupSubRate <= 0) {
            return;
        }
        $user->load('parent');
        $parent = $user->parent;
        if (!$parent) {
            return;
        }

        if ($groupSubRate < 1) {
            $userMoney = sprintf('%2.f', $price * $groupSubRate);
        } else {
            $userMoney = $groupSubRate;
        }
        $amount = $userMoney * $exchangeRate;


        $commissionAttributes = [
            'user_id' => $parent->getAttribute('id'),
            'child_user_id' => $user->getAttribute('id'),
            'child_user_withdraw_id' => 0,
            'child_user_money' => 0,
            'user_money' => $userMoney,
            'exchange_rate' => $exchangeRate,
            'amount' => $amount,
            'prize' => $groupSubRate,
            'child_user_withdraw_at' => Carbon::now(),
            'source' => CommissionEnum::SOURCE_GROUP_CASH,
        ];
        Commission::query()->create($commissionAttributes);
        UserWalletService::income($parent, $amount, Enum::WALLET_TYPE_COMMISSION);
    }

    // 团长奖励
    public static function byGroup(UserWithdraw $userWithdraw)
    {
        // 1、用app内邀请好友去邀请，下级就会有师傅id，在后台显示的就是有师傅间接收益，
        //  收益为两部分组成，后台百分之10永久提成+软件里面的收徒最高88奖励。
        //
        //2、用团长专属推广链接去邀请，这样下级就不会有师傅id，在后台就是无师傅直接收益，
        //  这类收益为百分之20永久提成，无收徒最高88奖励。

        // invite_prize 成员(间推)收益比例
        // direct_prize 专属(直推)收益比例
        // child_leader_price 从下级团长收益比例
        /** @var User $user */
        $user = $userWithdraw->user;
        /** @var GroupMember $member */
        $member = GroupMember::query()
            ->with(['group', 'leader'])
            ->where([
                'user_id' => $user->getAttribute('id'),
            ])->first();
        if (!$member) {
            return;
        }
        $leader = $member->leader;
        if (!$leader) {
            return;
        }
        /** @var User $parent */
        $parent = $user->parent;

        $group = Setting::get('group');
        if (!$group['enabled']) {
            return;
        }
        if (!$leader->is_advanced_group) {
            // 普通团长
            $invitePrize = $group['invite_prize'] ?? 0;
            $directPrize = $group['direct_prize'] ?? 0;
            $childLeaderPrice = $group['child_leader_price'] ?? 0;
        } else {
            $group = Setting::get('advanced_group');
            // 高级团长
            $invitePrize = $group['advanced_invite_prize'] ?? 0;
            $directPrize = $group['advanced_direct_prize'] ?? 0;
            $childLeaderPrice = $group['advanced_child_leader_price'] ?? 0;
        }

        if (empty($directPrize) || empty($invitePrize) || empty($childLeaderPrice)) {
            return;
        }

        DB::beginTransaction();
        try {
            $exchangeRate = Setting::get('exchange_rate');
            // 用户累计提现
            $childUserTotalMoney = UserWithdraw::query()
                ->where([
                    'user_id' => $userWithdraw->getAttribute('user_id'),
                    'status' => Enum::WITHDRAW_STATUS_APPROVED,
                ])
                ->sum('money');
            // 读取后台团长奖励设置
            if (!$parent) {
                // 如果没有师傅属于直推奖励
                $prize = $directPrize;
            } else {
                // 如果有师傅就属于间推奖励
                $prize = $invitePrize;
            }
            // 如果自身是团长对读取；下级团长奖励比例
            if ($user->is_group_leader) {
                $prize = $childLeaderPrice;
            }
            $userMoney = $userWithdraw->getAttribute('money') * $prize;
            $amount = $userMoney * $exchangeRate;
            $commissionAttributes = [
                'user_id' => $leader->id,
                'child_user_id' => $user->id,
                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),
                'child_user_money' => $childUserTotalMoney,
                'user_money' => $userMoney,
                'exchange_rate' => $exchangeRate,
                'amount' => $amount,
                'prize' => $prize,
                'child_user_withdraw_at' => Carbon::now(),
                'source' => CommissionEnum::SOURCE_GROUP_DIRECT,
            ];
            Commission::query()->create($commissionAttributes);
            UserWalletService::income($leader, $amount, Enum::WALLET_TYPE_COMMISSION);
            DB::commit();
        } catch (\Exception $exception) {
            Log::channel('groups')->error($exception->getMessage());
            DB::rollBack();
        }
    }

    // 当前用户提现对上级用户进行返现奖励
    // 只对直接上级
    public static function byNormal(UserWithdraw $userWithdraw)
    {
        /** @var User $childUser */
        $childUser = $userWithdraw->user;

        /** @var User $parentUser */
        $parentUser = $childUser->parent;

        // 无上级
        if (empty($parentUser)) {
            return;
        }
        if ($parentUser->parent_id == $childUser->id) {
            Log::error(sprintf('无效用户对应关系Users:(%s, %s)', $childUser->id, $parentUser->parent_id));
            return;
        }

        $earnings = Setting::get('earnings');
        $maxPerGuest = $earnings['max_per_guest'] ?? 0;//每邀请用户最大收益
        $b2aRatios = $earnings['b_to_a_ratios'] ?? [];//师徒奖励配置

        $totalEarningsByChild = Commission::query()
            ->where([
                'user_id' => $parentUser->getAttribute('id'),
                'child_user_id' => $childUser->getAttribute('id'),
                'source' => 'direct_invite',
            ])
            ->sum('user_money');  //以获取的师徒奖励

        if ($totalEarningsByChild >= $maxPerGuest && $maxPerGuest != 0) {
            return;
        }

        // 用户累计提现
        $childUserTotalMoney = UserWithdraw::query()
            ->where([
                'user_id' => $childUser->getAttribute('id'),
                'status' => Enum::WITHDRAW_STATUS_APPROVED,
            ])
            ->sum('money');
        $fullPrizes = [];
        $selected = [];
        foreach ($b2aRatios as $b2aRatio) {
            if ($childUserTotalMoney >= $b2aRatio['b_value']) {
                $selected = $b2aRatio;//当前的档位
                $fullPrizes[] = $b2aRatio;//之前的全部档位
            }
        }
//说明：若「A得佣金」为空则：A得佣金 = B提现金额 * 佣金比例
        if (empty($selected)) {
            Log::channel('invites')->error('未配置佣金比例');
            return;
        }

        DB::beginTransaction();
        try {
            foreach ($fullPrizes as $fullPrize) {
                $totalEarningsByChild = Commission::query()
                    ->where([
                        'user_id' => $parentUser->getAttribute('id'),
                        'child_user_id' => $childUser->getAttribute('id'),
                        'source' => 'direct_invite',
                    ])
                    ->sum('user_money');//以获取的师徒奖励

                if ($totalEarningsByChild >= $maxPerGuest && $maxPerGuest != 0) {
                    Log::channel('invites')->info(sprintf('User: %s -> %s: %s Maximum.', $childUser->getAttribute('uuid'), $parentUser->getAttribute('uuid'), $selected['b_value']));
                    DB::commit();
                    return;
                }

                $selected = $fullPrize;//每个档位
                $given = Commission::query()->where([
                    'user_id' => $parentUser->getAttribute('id'),
                    'child_user_id' => $childUser->getAttribute('id'),
                    'prize' => $selected['b_value'],
                ])->exists();//判断当前档位的奖金是否领取
                if ($given) {
                    Log::channel('invites')->info(sprintf('User: %s -> %s: %s Given.', $childUser->getAttribute('uuid'), $parentUser->getAttribute('uuid'), $selected['b_value']));
                    continue;
                }
                //当前档位配置
                $toARatio = $selected['ratio'];//佣金比例
                $toAValue = $selected['a_value'];//A得佣金
                $toBValue = $selected['b_value'];//B提现金额

                $userMoney = 0;
                //  若当前档位可得佣金不为空
                if ($toAValue != 0) {
                    $userMoney = $toAValue;//直接按佣金
                } else {
                    $userMoney = sprintf("%.2f", $selected['b_value'] * $toARatio);
                }
//                if ($userMoney < 0.1) {
//                    Log::error(sprintf('金额太小: %s', $userMoney));
//                    continue;
//                }

                $exchangeRate = Setting::get('exchange_rate');
                $amount = $exchangeRate * $userMoney;
                $commissionAttributes = [
                    'user_id' => $parentUser->getAttribute('id'),
                    'child_user_id' => $childUser->getAttribute('id'),
                    'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),
                    'child_user_money' => $childUserTotalMoney,
                    'user_money' => $userMoney,
                    'exchange_rate' => $exchangeRate,
                    'amount' => $amount,
                    'prize' => $selected['b_value'],
                    'child_user_withdraw_at' => Carbon::now(),
                    'source' => CommissionEnum::SOURCE_NORMAL_INVITE,
                    'remark' => '徒弟提现满' . $selected['b_value'] . '奖励' . $userMoney,
                ];
                Commission::query()->create($commissionAttributes);
                UserWalletService::income($parentUser, $amount, Enum::WALLET_TYPE_COMMISSION);
            }
            DB::commit();
        } catch (\Exception $exception) {
            Log::channel('invites')->error($exception->getMessage());
            DB::rollBack();
        }
    }

    /**
     * 上级团长是别人的团员
     * @param UserWithdraw $userWithdraw
     * @return void
     */
    public static function byUpperGroup(UserWithdraw $userWithdraw)
    {

        /** @var User $user */
        $user = $userWithdraw->user;
        /** @var GroupMember $member */
        $member = GroupMember::query()
            ->with(['group', 'leader'])
            ->where([
                'user_id' => $user->getAttribute('id'),
            ])->first();
        if (!$member) {
            return;
        }
        $leader = $member->leader;
        if (!$leader) {
            return;
        }

        //你的团长要是别人的团员
        $upperMember = GroupMember::query()
            ->with(['group', 'leader'])
            ->where([
                'user_id' => $leader->getAttribute('id'),
            ])
            ->where('leader_id', '!=', $leader->getAttribute('id'))
            ->first();

        if (!$upperMember) {

            return;
        }

        $upperLeader = $upperMember->leader;

        if (!$upperMember) {

            return;
        }

        $group = Setting::get('group');
        if (!$group['enabled']) {
            return;
        }
        if (!$upperLeader->is_advanced_group) {
            // 普通团长
            $prize = $group['son_prize'] ?? 0;
        } else {
            $group = Setting::get('advanced_group');
            // 高级团长
            $prize = $group['advanced_son_prize'] ?? 0;
        }

        if (empty($prize)) {
            return;
        }

        DB::beginTransaction();
        try {
            $exchangeRate = Setting::get('exchange_rate');
            // 用户累计提现
            $childUserTotalMoney = UserWithdraw::query()
                ->where([
                    'user_id' => $userWithdraw->getAttribute('user_id'),
                    'status' => Enum::WITHDRAW_STATUS_APPROVED,
                ])
                ->sum('money');

            $userMoney = $userWithdraw->getAttribute('money') * $prize;
            $amount = $userMoney * $exchangeRate;
            $commissionAttributes = [
                'user_id' => $upperLeader->id,
                'child_user_id' => $user->id,
                'child_user_withdraw_id' => $userWithdraw->getAttribute('id'),
                'child_user_money' => $childUserTotalMoney,
                'user_money' => $userMoney,
                'exchange_rate' => $exchangeRate,
                'amount' => $amount,
                'prize' => $prize,
                'child_user_withdraw_at' => Carbon::now(),
                'source' => CommissionEnum::SOURCE_UPPER_GROUP,
            ];
            Commission::query()->create($commissionAttributes);
            UserWalletService::income($upperLeader, $amount, Enum::WALLET_TYPE_COMMISSION);
            DB::commit();
        } catch (\Exception $exception) {
            Log::channel('groups')->error($exception->getMessage());
            DB::rollBack();
        }
    }
}
